/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import pojo.Customer;

/**
 *
 * @author Hagar
 */
public class RegisterServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
             Session session = sessionFactory.openSession();

   /* public List findCustomer(Customer customer) {

        Example exampleCustomer = Example.create(customer).enableLike(MatchMode.EXACT);
        return session.createCriteria(Customer.class).add(exampleCustomer).list();
    }*/

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
             System.out.println("inside register");
             

           
            String s1 = request.getParameter("fName");
            String s2 = request.getParameter("lName");
            String s3 = request.getParameter("birthdate");
            String s4 = request.getParameter("email");
            String s5 = request.getParameter("password");
            String s6 = request.getParameter("credit");
            
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-dd-mm");
            Date convertedDate;
        
            convertedDate = dateFormat.parse(s3);
        

            List n=session.createCriteria(Customer.class).setProjection(Projections.max("id")).list();
            int a=(int)n.iterator().next();
            Customer customer=new Customer();
            customer.setEmail(s4);

            List result = session.createCriteria(Customer.class).add(Restrictions.like("email", s4)).setProjection(Projections.property("email")).list();
          // List result=findCustomer(customer);
            if (result.isEmpty()) {
                
                Customer c = new Customer(a+1,s1,s2,s4,s5,"customer",convertedDate,Float.parseFloat(s6));
                session.beginTransaction();
                session.persist(c);
                session.getTransaction().commit();
                HttpSession s=request.getSession(true);
                s.setAttribute("user",c);
                response.sendRedirect("home.jsp");
            } 
            else 
            {
                
                response.sendRedirect("Register.jsp?m=This mail is already registered");
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {       
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
            
    }
    

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
